Jenkins Kata 3: Automated Testing
Learn how to make a step, make the test fail, and fix the code.
We'll cover the following
Test automation is a critically important function of CI.
Many of the common failures encountered during software development can be prevented through automated testing. Automated tests can, for example:
- Validate the output of isolated system components
- Inspect code for common security vulnerabilities
- Test the runtime behavior of an application
This kata will demonstrate how to add a simple test to a Jenkins job.
Step 1: Add a build step to run the test#
The following are the steps to add a build to run the test:
- Click “Configure.”
- Click the “Build Steps” tab.
- Click “Add build step.”
- Select “Execute shell.”
- Enter the following command:
- Click “Save”
- Click “Build Now.”
This test uses the listvalidator.py Python program to validate the Storelist. The Coder family is particular about what they buy from the store, so they add approved purchases to the Main List (otherwise, Cody has a habit of buying ice cream).
The listvalidator.py program reads the items from the Main List and the Next Visit list. Those who’re familiar with Python can open the program and take a look. It’s not important to understand the code but rather to understand what it does.
If all the items in the Next Visit list are also listed in the Main List, the program exits with a zero return code, indicating success. If, however, someone attempts to sneak an unapproved item into the Next Visit list, the program exits with a one. This causes the Jenkins job to fail.
Step 2: Make the test fail#
To do this:
- Switch to the terminal window.
- Change to Cody’s local repository and edit
storelist.htm:
- Enter an item in the Next Visit list that’s not listed in the Main List.
- Save the changes.
-
Commit and push the change:
- Return to the terminal window.
- Commit the change to
storelist.htm, as follows:
- Push the change to the Git server, as follows:
-
Run the job:
- Return to Jenkins.
- Click “Build Now.”
-
View the job output:
- Select the failed build from the build history.
- Select “Console Output.”
Adding an invalid item to the Next Visit list does exactly what we need it to: It fails the build. This is a simple example of a test that can be automated by Jenkins.
Step 3: Fix the code#
To reverse the change:
- Return to the terminal window.
- Revert the addition of the invalid item as follows:
- Push the change to Gogs as follows:
-
To run the job:
- Return to Jenkins.
- Click “Build Now.”
The git revert command reverses the previous change, which added the invalid item. We then use git push to push the fix to Gogs and then build the job in Jenkins. The Jenkins job is successful again now that the items in the Next Visit list match with items in the Main List.
Jenkins Kata 2: Build Steps
Jenkins Kata 4: Integrate Git with Jenkins